/*
 * @lc app=leetcode.cn id=56 lang=cpp
 *
 * [56] 合并区间
 */

// @lc code=start
class Solution
{
    static bool cmp(const vector<int> &a, const vector<int> &b)
    {
        if (a[0] == b[0])
        {
            return a[1] < b[1];
        }
        else
        {
            return a[0] < b[0];
        }
    }

public:
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        //合并区间 首先排序
        sort(intervals.begin(), intervals.end(), cmp);
        vector<vector<int>> res;
        vector<int> path = {intervals[0][0], intervals[0][1]};
        for (int i = 1; i < intervals.size(); i++)
        {
            if (intervals[i][0] > path[1])
            {
                //不相交
                res.push_back(path);
                path[0] = intervals[i][0];
                path[1] = intervals[i][1];
            }
            else
            {
                //相交合并区间
                path[1] = max(intervals[i][1],path[1]);
            }
        }
        res.push_back(path);
        return res;
    }
};
// @lc code=end
